package com.example.child_protection.service.impl;

import com.example.child_protection.dao.UserDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.UserInsertRequestDTO;
import com.example.child_protection.dto.request.update.UserUpdatePwdRequestDTO;
import com.example.child_protection.dto.request.update.UserUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.enums.UserStatesEnums;
import com.example.child_protection.exception.MyBizException;
import com.example.child_protection.model.UserModel;
import com.example.child_protection.service.UserManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户信息管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class UserManageServiceImpl extends MyBaseService implements UserManageService {

    @Autowired
    public UserDao userDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(UserInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.USER_INSERT, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<UserInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(UserInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                UserModel model = new UserModel();
                BeanUtils.copyProperties(requestDTO,model);
                model.setStates(UserStatesEnums.NORMAL);
                userDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(UserUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.USER_UPDATE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<UserUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(UserUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                UserModel model = new UserModel();
                BeanUtils.copyProperties(requestDTO,model);
                userDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service(ServiceActionEnums.USER_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                UserModel userModel = new UserModel();
                BeanUtils.copyProperties(requestDTO,userModel);
                userDao.deleteForLogic(userModel);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改密码
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO updatePwd(UserUpdatePwdRequestDTO requestDTO){

        return this.service(ServiceActionEnums.USER_PWD_UPDATE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<UserUpdatePwdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(UserUpdatePwdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                UserModel req = new UserModel();
                req.setId(requestDTO.getId());
                req.setUserPwd(requestDTO.getOldPwd());
                int result = userDao.count(req);
                if(result>0){
                    req.setUserPwd(requestDTO.getNewPwd());
                    userDao.updateSelective(req);
                }else{
                    throw new MyBizException("原密码错误");
                }
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }
}
